import { defineStore } from 'pinia'
import type { Medicine, MedicineQuery, MedicineForm } from '@/types/medicine'

// 模拟数据
const mockMedicines: Medicine[] = [
  {
    id: 1,
    code: 'M001',
    name: '阿莫西林胶囊',
    categoryId: 1,
    categoryName: '处方药',
    specification: '0.25g*24粒',
    unit: '盒',
    manufacturer: '哈药集团制药总厂',
    price: 15.8,
    stock: 1000,
    minStock: 100,
    status: 'normal',
    remark: '用于敏感菌所致的感染',
    createTime: '2024-03-18 10:00:00',
    updateTime: '2024-03-18 10:00:00'
  },
  {
    id: 2,
    code: 'M002',
    name: '布洛芬缓释胶囊',
    categoryId: 2,
    categoryName: '非处方药',
    specification: '0.3g*10粒',
    unit: '盒',
    manufacturer: '上海医药集团',
    price: 22.5,
    stock: 500,
    minStock: 50,
    status: 'normal',
    remark: '用于缓解轻至中度疼痛',
    createTime: '2024-03-18 10:00:00',
    updateTime: '2024-03-18 10:00:00'
  }
]

export const useMedicineStore = defineStore('medicine', {
  state: () => ({
    list: [...mockMedicines] as Medicine[],
    total: mockMedicines.length,
    loading: false,
    currentMedicine: null as Medicine | null
  }),

  actions: {
    // 获取药品列表
    async fetchList(query: MedicineQuery) {
      this.loading = true
      try {
        // 模拟搜索和分页
        let filteredList = [...mockMedicines]
        
        // 搜索过滤
        if (query.name) {
          filteredList = filteredList.filter(item => 
            item.name.toLowerCase().includes(query.name!.toLowerCase())
          )
        }
        if (query.categoryId) {
          filteredList = filteredList.filter(item => 
            item.categoryId === query.categoryId
          )
        }
        if (query.status) {
          filteredList = filteredList.filter(item => 
            item.status === query.status
          )
        }

        // 计算总数
        this.total = filteredList.length

        // 分页
        const start = (query.pageNum - 1) * query.pageSize
        const end = start + query.pageSize
        this.list = filteredList.slice(start, end)

        // 模拟网络延迟
        await new Promise(resolve => setTimeout(resolve, 300))
      } finally {
        this.loading = false
      }
    },

    // 获取药品详情
    async fetchDetail(id: number) {
      const medicine = mockMedicines.find(item => item.id === id)
      if (!medicine) {
        throw new Error('药品不存在')
      }
      this.currentMedicine = medicine
      return medicine
    },

    // 新增药品
    async add(medicine: MedicineForm) {
      // 模拟生成ID
      const newId = Math.max(...mockMedicines.map(item => item.id)) + 1
      
      const newMedicine: Medicine = {
        ...medicine,
        id: newId,
        categoryName: this.getCategoryName(medicine.categoryId),
        stock: 0,
        createTime: new Date().toLocaleString(),
        updateTime: new Date().toLocaleString()
      }

      mockMedicines.push(newMedicine)
      await new Promise(resolve => setTimeout(resolve, 300))
    },

    // 更新药品
    async update(id: number, medicine: MedicineForm) {
      const index = mockMedicines.findIndex(item => item.id === id)
      if (index === -1) {
        throw new Error('药品不存在')
      }

      mockMedicines[index] = {
        ...mockMedicines[index],
        ...medicine,
        categoryName: this.getCategoryName(medicine.categoryId),
        updateTime: new Date().toLocaleString()
      }

      await new Promise(resolve => setTimeout(resolve, 300))
    },

    // 删除药品
    async delete(id: number) {
      const index = mockMedicines.findIndex(item => item.id === id)
      if (index === -1) {
        throw new Error('药品不存在')
      }

      mockMedicines.splice(index, 1)
      await new Promise(resolve => setTimeout(resolve, 300))
    },

    // 更新药品状态
    async updateStatus(id: number, status: string) {
      const medicine = mockMedicines.find(item => item.id === id)
      if (!medicine) {
        throw new Error('药品不存在')
      }

      medicine.status = status as 'normal' | 'disabled'
      medicine.updateTime = new Date().toLocaleString()
      await new Promise(resolve => setTimeout(resolve, 300))
    },

    // 根据分类ID获取分类名称
    getCategoryName(categoryId: number): string {
      const categories = {
        1: '处方药',
        2: '非处方药',
        3: '中药',
        4: '西药'
      }
      return categories[categoryId as keyof typeof categories] || '未知分类'
    }
  }
}) 